(function(){
    function app() {
        // 获取产品数据
        this.getProduct = function(pid) {
            var data = this._get('/app/index.php?d=api&c=product&m=get_product&product_id='+pid);
            return data.product || null;
        }

        // 微博分享产品
        this.wbShareProduct = function(pid, text) {
            return this._post('/app/index.php?d=api&c=weibo&m=share_product', 
                {product_id: pid, text: text});
        }

        // 微博登陆
        this.wbLogin = function() {
            $.oauthpopup({path: '/app/index.php?c=weibo&m=login'});
        }

        // 微博退出
        this.wbLogout = function() {
            $.oauthpopup({path: '/app/index.php?c=weibo&m=logout'});
        }

        // 获取当前用户信息
        this.getUserInfo = function() {
            var user = this._get('/app/index.php?d=api&c=user_controller&m=get_user_info');
            return typeof user.error == 'undefined' ? user : null;
        }

        this.user = this.getUserInfo() || null;
    }

    app.prototype = {
        _ajax: function(url, submit, method, dataType) {
            var result;
            $.ajax({
                url:        url,
                data:       submit || {},
                type:       method || 'GET',
                dataType:   dataType || 'json',
                async:      false,
                cache:      false,
                complete:   function(xml) {},
                success:    function(str) {
                    result = str
                }
            })
            return result;
            // return typeof result['error'] == 'undefined' ? result : false;
        },
        _get: function(url, submit) {
            return this._ajax(url, submit, 'GET');
        },
        _post: function(url, submit) {
            return this._ajax(url, submit, 'POST');
        },
        redirect: function(url) {
            window.location.href = url;
        }
    }

    window.app = new app();

})();
